package com.coke.demo.mapper;

import com.coke.demo.model.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 19602
 */
@Component
public interface UserMapper {

    /**
     * 检测用户Id与密码是否正确
     *
     * @param userid:用户的id
     * @return user
     */
    @Select("select * from laboratory.user where UserId=#{userid}")
    User checkUser(int userid);

    /**
     * 用户注册
     *
     * @param user:用户注册填写的信息
     * @return true
     */
    @Insert("insert into user values (#{userid}, #{username}, #{password}, #{sex}, #{faculty},#{priority})")
    Boolean insertUser(User user);

    /**
     * 查询所有管理员信息
     *
     * @return List<User>
     */
    @Select("select * from user where priority=2")
    List<User> findAllAdmin();

    /**
     * 查询所有用户信息
     *
     * @return List<User>
     */
    @Select("select * from user")
    List<User> findAllUsers();

    /**
     * 查询所有学生信息
     *
     * @return List<User>
     */
    @Select("select * from user where priority=0")
    List<User> findAllStudents();

    /**
     * 查询所有教师信息
     *
     * @return List<User>
     */
    @Select("select * from user where priority=1")
    List<User> findAllTeachers();

    /**
     * 根据id查询用户信息
     *
     * @param userid:用户的id
     * @return user
     */
    @Select("select * from user where UserId=#{userid}")
    User findById(Integer userid);

    /**
     * 根据用户id删除用户信息
     *
     * @param userid :用户id
     * @return Boolean
     */
    @Delete("delete from user where UserId=#{userid}")
    Boolean deleteById(Integer userid);

    /**
     * 修改用户信息
     *
     * @param user:用户信息
     * @return true
     */
    @Update("update user set UserName=#{username},Password=#{password},Sex=#{sex},Faculty=#{faculty},Priority=#{priority} where UserId=#{userid}")
    Boolean updateUserInfo(User user);

    /**
     * 获取不同权限用户的数量
     *
     * @param priority 用户权限
     * @return Integer
     */
    @Select("select count(1) from user where priority=#{priority}")
    Integer countUser(Integer priority);

    /**
     * 根据姓名查询用户信息
     *
     * @param username:用户的姓名
     * @return user
     */
    @Select("select * from laboratory.user where UserName like '%${username}%'")
    ArrayList<User> queryUserByName(String username);

}
